define(function (require, exports, module) {
    var $ = require('jquery');

    var angular = require('angular');
    require('business_modules/material/modules/common');
    require('business_modules/material/modules/directives/pagination');

    var mass_message = angular.module('mass-message',['ngRoute','common','directives']);

    mass_message.config(function ($routeProvider) {
        $routeProvider
            .when('/mass-message', {
                controller: 'createMassMessage',
                templateUrl: "mass-message/views/message_new.html"
            })
            .when('/mass-message/sended', {
                controller: 'sendedMassMessage',
                templateUrl: "mass-message/views/message_sended.html"
            })
            .otherwise({
                redirectTo: '/mass-message'
            });
    })
    .factory('massMessageService',[
        'http',
        function (http) {
            return {
                createMassMessage: function (options) {
                    http.post('mass-message/send',options.data,options.search)
                        .success(function (res) {
                            if (res.result || res.retCode === 0) {
                                return options.success(res);
                            }
                            options.fail && options.fail(res);
                        })
                        .error(function (res) {
                            options.fail && options.fail(res);
                        });
                },
                undoMassMessage:function (options) {
                    http.post('mass-message/cancel',options.data,options.search)
                        .success(function (res) {
                            if (res.result || res.retCode === 0) {
                                return options.success(res);
                            }
                            options.fail && options.fail(res);
                        })
                        .error(function (res) {
                            options.fail && options.fail(res);
                        });
                },
                getMessageList: function (options) {
                    http.get('material/mpnews-list',options.search)
                        .success(function (res) {
                            if (res.result || res.retCode == 0) {
                                return options.success(res);
                            }
                            options.fail && options.fail(res);
                        })
                        .error(function (res) {
                            options.fail && options.fail(res);
                        });
                },
                getSendedMessageList: function (options) {
                    http.post('mass-message/log-list',options.data)
                        .success(function (res) {
                            if (res.result || res.retCode === 0) {
                                return options.success(res);
                            }
                            options.fail && options.fail(res);
                        })
                        .error(function (res) {
                            options.fail && options.fail(res);
                        });
                }
            };
        }
    ])
    .controller('createMassMessage',[
        '$scope',
        '$rootScope',
        '$compile',
        'massMessageService',
        'limit',
        '$location',
        function ($scope,$rootScope,$compile,service,limit,$location) {

            //和弹窗选择相关的操作集合
            $scope.messageSelector = {
                init: function ($dialogScope,$dialog) {
                    $dialogScope.messageSelector = $scope.messageSelector;
                    $scope.dialog = $dialog;
                    $scope.searchMessageList(1);
                },
                create: function ($dialog) {
                    $scope.dialog.close();
                    $location.path('/material/img_text/edit');
                },
                selected: undefined,
                select: function (index) {
                    $scope.messageSelector.selected = index;
                },
                search: {
                    keyword: "",
                    reset: function () {
                        $scope.messageSelector.search.keyword = "";
                    },
                    submit: function () {
                        $scope.searchMessageList(1,$scope.messageSelector.search.keyword);
                    }
                },
                add: function () {
                    var selected = $scope.messageSelector.selected;
                    if (selected !== undefined) {
                       $scope.message = $scope.messageSelector.messageList[selected];
                    }
                },
                pagination : {
                    pages : 0,
                    current : 1,
                    change: function (page) {
                        $scope.messageSelector.selected = undefined;
                        $scope.searchMessageList(page);
                    }
                }
            };

            $scope.searchMessageList = function (page,keyword) {
                service.getMessageList({
                    search: {
                        keyword: keyword || "",
                        limit: limit,
                        offset: limit * (page - 1)
                    },
                    success: function (res) {
                        $scope.messageSelector.messageList = res.data.data;
                        $scope.messageSelector.pagination.pages = Math.ceil(res.data.total / limit);
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','获取图文消息失败');
                    }
                });
            };

            $scope.createMassMessage = function () {
                if (!$scope.message) {
                    $rootScope.$emit('httpRequestFail','请选择素材');
                    return;
                }
                service.createMassMessage({
                    data: {
                        msg_type: "mpnews",
                        content: $scope.message.media_id
                    },
                    success: function (res) {
                        $rootScope.$emit('httpRequestSuccess','发送成功');
                        $location.path('/mass-message');
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','群发图文消息失败');
                    }
                });
            };

            $scope.deleteMessage = function () {
                $scope.message = null;
                $scope.messageSelector.selected = undefined;
            };

        }
    ])
    .controller('sendedMassMessage',[
        '$scope',
        '$rootScope',
        'massMessageService',
        'limit',
        function ($scope,$rootScope,service,limit) {

            $scope.pagination = {
                pages : 0,
                current : 1,
                change: function (page) {
                    $scope.searchVideoList(page);
                }
            };

            $scope.searchSendedMessageList = function (page) {
                service.getSendedMessageList({
                    data: {
                        page: page,
                        pageSize: limit
                    },
                    success: function (res) {
                        $scope.messageList = res.data.items;
                        $scope.pagination.pages = Math.ceil(res.data.total / limit);
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','获取消息列表失败');
                    }
                });
            };

            $scope.undoMessage = function (messageId) {
                service.undoMassMessage({
                    search: {
                        id: messageId
                    },
                    success: function (res) {
                        $scope.searchSendedMessageList($scope.pagination.current);
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','撤销失败');
                    }
                });
            };

            $scope.searchSendedMessageList(1);
        }
    ])
});
